package cn.edu.njust.epms.service.Impl;

import cn.edu.njust.epms.dao.ATDao;
import cn.edu.njust.epms.dao.UserDao;
import cn.edu.njust.epms.entity.ATRecord;
import cn.edu.njust.epms.entity.ATResv;
import cn.edu.njust.epms.entity.UserDetails;
import cn.edu.njust.epms.service.ATService;
import cn.edu.njust.epms.common.R;
import cn.edu.njust.epms.common.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class ATServiceImpl implements ATService {
    @Autowired
    ATDao dao;
    @Autowired
    UserDao udao;

    /**
     * 核酸检测：
     * 防控人员（uSts>=1）在平台中输入用户信息，登记检测
     * @param pid
     * @return
     */
    @Override
    public Resp<ATRecord> acidTest(String pid) {
//        String pid = record.getPid();
        UserDetails ud = udao.selectUdByPid(pid);
        if(ud==null)    return Resp.error("请输入正确的身份证！");
        ATRecord record = new ATRecord();
//        System.out.println(pid);
        record.setPid(pid);
        record.setaTime(new Date());
        record.setaResult(1);//result:1--已采样，2--待复核，3-阴性，4-阳性
//        System.out.println(record.toString());
        dao.Add(record);
        return Resp.success(record);
    }


    @Override
    public Resp<List<ATRecord>> selectByCondition(R r){
        if(!(r.getName() == null||r.getName().equals("")||"".equals(r.getName())))
            r.setName("%"+r.getName()+"%");
        if(!(r.getPid() == null||r.getPid().equals("")||"".equals(r.getPid())))
            r.setPid("%"+r.getPid()+"%");
        List<ATRecord> list = dao.selectByCondition(r);
        return Resp.success(list);
    }


    @Override
    public Resp<ATRecord> updateATResult(ATRecord record) {
        ATRecord record1 = dao.selectByAid(record.getaId());
        if(record1.getaResult()==3||record1.getaResult()==4)
            return Resp.error("已经录入结果，不能修改！");
        boolean b = dao.updateATResult(record.getaResult(), record.getaId());
        if (b)  return Resp.success(record);
        return Resp.error("信息更新失败！");
    }

    @Override
    public Resp<ATResv> acidTestResv(ATResv ar) {
        if(ar.getaTime()==null){
            return Resp.error("请输入时间！");
        }
        if (ar.getPid()==null){
            return Resp.error("请输入身份证号！");
        }
        ar.setrTime(new Date());
        ar.setSts(0);
        dao.addResv(ar);
        return Resp.success(ar);
    }

    @Override
    public Resp<ATResv> updateATResv(ATResv ar) {
        Boolean b = dao.updateResv(ar);
        if (b)  return Resp.success(ar);
        return Resp.error("信息更新失败！");
    }

    @Override
    public Resp<List<ATResv>> selectResvByCondition(R r){
        if(!(r.getName() == null||r.getName().equals("")||"".equals(r.getName())))
            r.setName("%"+r.getName()+"%");
        if(!(r.getPid() == null||r.getPid().equals("")||"".equals(r.getPid())))
            r.setPid("%"+r.getPid()+"%");
        List<ATResv> list = dao.selectResvByCondition(r);
        return Resp.success(list);
    }


    @Override
    public Resp<String> deleteATRecord(Integer aId){
        Boolean b = dao.deleteATRecord(aId);
        if (b)  return Resp.success("删除成功");
        return Resp.error("删除失败！");
    }

    @Override
    public Resp<String> deleteATResv(ATResv r){
        Boolean b = dao.deleteATResv(r);
        if (b)  return Resp.success("删除成功");
        return Resp.error("删除失败！");
    }

    @Override
    public List<UserDetails> selectUdNotAT7day() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        Date date1 = calendar.getTime();
        calendar.add(Calendar.DATE,-7);
//        SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        List<UserDetails> list = dao.selectUdWithoutAT(calendar.getTime(), date1);
        return list;
    }

}
